package test.test.one.levels;

import test.test.one.MainThread;
import test.test.one.R;
import test.test.one.TestActivity;
import test.test.one.objects.ob_character;
import test.test.one.objects.ob_exit;
import test.test.one.objects.ob_inventory;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * AbstractLevel info
 * @author Gustavo
 * This abstract class is to be extended to create game levels
 */
//TODO missing a lot of documentation
public abstract class AbstractLevel extends SurfaceView 
implements SurfaceHolder.Callback {
	
	private static final String TAG = AbstractLevel.class.getSimpleName(); //Class TAG
	
	/*
	 * variable declaration
	 */
	
	protected MainThread thread;
	protected ob_exit bt_exit;
	
	
	//protected ob_player player;
	protected ob_character player;
	
	
	protected ob_inventory bag;
	protected float new_pos;
	protected Bitmap background;
	
	public AbstractLevel(Context context){
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);
		
		this.thread=new MainThread(getHolder(), this);
		this.bag=new ob_inventory(BitmapFactory.decodeResource(getResources(), R.drawable.mocla), TestActivity.GAMEWIDTH-30, 30);
		this.bt_exit=new ob_exit(BitmapFactory.decodeResource(getResources(), R.drawable.x), 20, 20);
		//this.player=new ob_player(BitmapFactory.decodeResource(getResources(), R.drawable.mono), 
			//	TestActivity.GAMEWIDTH/2, TestActivity.GAMEHEIGHT-100,2);
		this.player = new ob_character(
				//null
				BitmapFactory.decodeResource(getResources(), R.drawable.p1)
				, TestActivity.GAMEWIDTH/2, TestActivity.GAMEHEIGHT-160	// initial position
				, 30, 47	// width and height of sprite
				, 5, 6		// FPS and number of frames in the animation
				, 2);		//inventory
		this.background=BitmapFactory.decodeResource(getResources(), R.drawable.mapa);

		
		setFocusable(true);
	}
	
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		thread.setRunning(true);
		thread.start();
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
				Log.d(TAG, "retry is false");
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}
	
	@Override
	public abstract boolean onTouchEvent(MotionEvent event);

	public void render(Canvas canvas){
		canvas.drawBitmap(background,0,0,null);		
		bt_exit.draw(canvas);
		bag.draw(canvas);
		player.draw(canvas);
	}
	public abstract void update();
	
	
}
